<?php

/**
 * cliente actions.
 *
 * @package    courier
 * @subpackage cliente
 * @author     Your name here
 */
class clienteActions extends sfActions
{
  #Funcion que ejecuta el index del usuario registado, busca su correo en la base de datos
  public function executeIndex(sfWebRequest $request) {
	$email = $request->getParameter('openid_ext1_value_email');
	$criteria= new Criteria();
	#compara el valor de $email con el campo de email en la tabla cliente de la base datos	
	$criteria->add(ClientePeer::CLI_CTAGOOGLE,$email);
	#del query solo tomar un resultado, y lo guardamos en $cliente	
	$cliente=ClientePeer::doSelectOne($criteria);
		if($cliente!=''){
			$this->getUser()->setAuthenticated(true);
			$this->getUser()->setAttribute('cliente_id',$cliente->getCliId());
			$this->getUser()->setAttribute('cliente_nombre',$cliente->getCliNombre().' '.$cliente->getCliApellido());
		}

  }
	
	public function executeLogout (sfWebRequest $request){
		$this->getUser()->setAuthenticated(false);
		$this->getUser()->getAttributeHolder()->clear();
		$this->redirect('@homepage');
	}

  public function executeShow(sfWebRequest $request)
  {
    $this->Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id'));
    $this->forward404Unless($this->Cliente);
  }

  public function executeShowTarjeta(sfWebRequest $request)
  {
    $this->Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id'));
    $this->forward404Unless($this->Cliente);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new ClienteForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new ClienteForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id')), sprintf('Object Cliente does not exist (%s).', $request->getParameter('cli_id')));
    $this->form = new ClienteForm($Cliente);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id')), sprintf('Object Cliente does not exist (%s).', $request->getParameter('cli_id')));
    $this->form = new ClienteForm($Cliente);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($Cliente = ClientePeer::retrieveByPk($request->getParameter('cli_id')), sprintf('Object Cliente does not exist (%s).', $request->getParameter('cli_id')));
    $Cliente->delete();

    $this->redirect('cliente/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $Cliente = $form->save();

      $this->redirect('metodopago/newMetodoCliente?cli_id='.$Cliente->getCliId());
    }
  }
}
